package com.bugbuff.dubbo.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bugbuff.common.pojo.PageResult;
import com.bugbuff.common.vo.ErrorResult;
import com.bugbuff.dubbo.server.service.MyCenterService;
import com.bugbuff.dubbo.server.vo.CountVo;
import com.bugbuff.dubbo.server.vo.UserInfoVo;
import com.bugbuff.dubbo.server.vo.UserSettingsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("users")
@RestController
@Slf4j
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id，如果为空，表示查询当前登录人的信息
     * @return
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> queryUserInfoByUserId(@RequestParam(value = "userID", required = false) Long userId) {
        try {
            UserInfoVo userInfoVo = this.myCenterService.queryUserInfoByUserId(userId);
            if (ObjectUtil.isNotEmpty(userInfoVo)) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("根据用户id查询用户信息出错~ userId = " + userId, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户修改头像上传
     *
     * @param file
     * @return
     */
    @PostMapping("header")
    public ResponseEntity<Void> saveHeadPhoto(@RequestParam("headPhoto") MultipartFile file) {
        try {
            Boolean result = myCenterService.saveHeadPhoto(file);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("用户头像上传失败", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户修改信息保存
     *
     * @param userInfoVo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateUserInfo(@RequestBody UserInfoVo userInfoVo) {
        Boolean result = myCenterService.saveUserInfoData(userInfoVo);
        if (result) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 数查询互相喜欢总数、喜欢总数、粉丝总
     *
     * @return
     */
    @GetMapping("counts")
    public ResponseEntity<CountVo> queryCounts() {
        try {
            CountVo countVo = myCenterService.queryCounts();
            if (ObjectUtil.isNotEmpty(countVo)) {
                return ResponseEntity.ok(countVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 根据类型查询喜欢列表
     *
     * @param type
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity<PageResult> queryLikeList(@PathVariable("type") String type,
                                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize,
                                                    @RequestParam(value = "nickname", required = false) String nickname) {
        try {
            if(StringUtils.isNotEmpty(nickname)){
                page=1;
            }
            PageResult pageResult = myCenterService.queryLikeListByType(type, page, pagesize, nickname);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 修改手机号--发送短信验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity<Void> sendVerificationCode() {
        try {
            Boolean result = myCenterService.sendVerificationCode();
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 修改手机号--校验验证码
     *
     * @param param
     * @return
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity<Map<String, Object>> checkVerificationCode(@RequestBody Map<String, String> param) {
        String verificationCode = param.get("verificationCode");
        Boolean result = myCenterService.checkVerificationCode(verificationCode);
        Map<String, Object> map = new HashMap<>();
        map.put("verification", result);
        return ResponseEntity.ok(map);
    }


    /**
     * 修改手机号--保存
     *
     * @param param
     * @return
     */
    @PostMapping("phone")
    public ResponseEntity<ErrorResult> updatePhone(@RequestBody Map<String, String> param) {
        String phone = param.get("phone");
        ErrorResult errorResult = myCenterService.updatePhone(phone);
        if (errorResult==null) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }


    /**
     * 通用设置查看
     *
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity<UserSettingsVo> queryUserSeetings() {
        UserSettingsVo userSettingsVo = myCenterService.queryUserSeetings();
        if (userSettingsVo != null) {
            return ResponseEntity.ok(userSettingsVo);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 陌生人问题更改
     *
     * @param map
     * @return
     */
    @PostMapping("questions")
    public ResponseEntity<Void> setQuestions(@RequestBody Map<String, String> map) {
        String content = map.get("content");
        Boolean result = myCenterService.setQuestions(content);
        if (result) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 通知状态管理
     *
     * @param map
     * @return
     */
    @PostMapping("notifications/setting")
    public ResponseEntity<Void> setNotice(@RequestBody Map<String, Boolean> map) {
        Boolean pinglunNotification = map.get("pinglunNotification");
        Boolean likeNotification = map.get("likeNotification");
        Boolean gonggaoNotification = map.get("gonggaoNotification");
        boolean result = myCenterService.setNotice(pinglunNotification, likeNotification, gonggaoNotification);
        if (result) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 黑名单列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("blacklist")
    public ResponseEntity<PageResult> queryBlackList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = myCenterService.queryBlackList(page, pageSize);
        if (pageResult != null) {
            return ResponseEntity.ok(pageResult);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 黑名单取消
     *
     * @param userId
     * @return
     */
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity<Void> deleteBlack(@PathVariable("uid") Long userId) {
        boolean result = myCenterService.deleteBlack(userId);
        if (result) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 粉丝===喜欢
     * @param userId
     * @return
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity<Void> fansLike(@PathVariable("uid") Long userId) {

        Boolean result = myCenterService.fansLike(userId);
        if (result) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 粉丝===取消喜欢
     * @param likeUserId
     * @return
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity<Void> disLike(@PathVariable("uid") Long likeUserId) {
        try {
            myCenterService.disLike(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
